package co.yixiang.yshop.module.product.service.storeproductassureref;

import cn.hutool.core.util.ObjectUtil;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.module.product.controller.admin.storeproductassure.vo.StoreProductAssureRespVO;
import co.yixiang.yshop.module.product.controller.admin.storeproductassureref.vo.StoreProductAssureRefCreateReqVO;
import co.yixiang.yshop.module.product.controller.admin.storeproductassureref.vo.StoreProductAssureRefExportReqVO;
import co.yixiang.yshop.module.product.controller.admin.storeproductassureref.vo.StoreProductAssureRefPageReqVO;
import co.yixiang.yshop.module.product.controller.admin.storeproductassureref.vo.StoreProductAssureRefUpdateReqVO;
import co.yixiang.yshop.module.product.convert.storeproductassure.StoreProductAssureConvert;
import co.yixiang.yshop.module.product.convert.storeproductassureref.StoreProductAssureRefConvert;
import co.yixiang.yshop.module.product.dal.dataobject.storeproductassure.StoreProductAssureDO;
import co.yixiang.yshop.module.product.dal.dataobject.storeproductassureref.StoreProductAssureRefDO;
import co.yixiang.yshop.module.product.dal.mysql.storeproductassureref.StoreProductAssureRefMapper;
import co.yixiang.yshop.module.product.service.storeproductassure.StoreProductAssureService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.product.enums.ErrorCodeConstants.STORE_PRODUCT_ASSURE_REF_NOT_EXISTS;

/**
 * 商品保障服务关联 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class StoreProductAssureRefServiceImpl implements StoreProductAssureRefService {

    @Resource
    private StoreProductAssureRefMapper storeProductAssureRefMapper;

    @Resource
    private StoreProductAssureService storeProductAssureService;

    @Override
    public Long createStoreProductAssureRef(StoreProductAssureRefCreateReqVO createReqVO) {
// 插入
        StoreProductAssureRefDO storeProductAssureRef = StoreProductAssureRefConvert.INSTANCE.convert(createReqVO);
        storeProductAssureRefMapper.insert(storeProductAssureRef);
// 返回
        return storeProductAssureRef.getId();
    }

    @Override
    public void updateStoreProductAssureRef(StoreProductAssureRefUpdateReqVO updateReqVO) {
// 校验存在
        validateStoreProductAssureRefExists(updateReqVO.getId());
// 更新
        StoreProductAssureRefDO updateObj = StoreProductAssureRefConvert.INSTANCE.convert(updateReqVO);
        storeProductAssureRefMapper.updateById(updateObj);
    }

    @Override
    public void deleteStoreProductAssureRef(Long id) {
// 校验存在
        validateStoreProductAssureRefExists(id);
// 删除
        storeProductAssureRefMapper.deleteById(id);
    }

    private void validateStoreProductAssureRefExists(Long id) {
        if (storeProductAssureRefMapper.selectById(id) == null) {
            throw exception(STORE_PRODUCT_ASSURE_REF_NOT_EXISTS);
        }
    }

    @Override
    public StoreProductAssureRefDO getStoreProductAssureRef(Long id) {
        return storeProductAssureRefMapper.selectById(id);
    }

    @Override
    public List
            <StoreProductAssureRefDO> getStoreProductAssureRefList(Collection<Long> ids) {
        return storeProductAssureRefMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult
            <StoreProductAssureRefDO> getStoreProductAssureRefPage(StoreProductAssureRefPageReqVO pageReqVO) {
        return storeProductAssureRefMapper.selectPage(pageReqVO);
    }

    @Override
    public List
            <StoreProductAssureRefDO> getStoreProductAssureRefList(StoreProductAssureRefExportReqVO
                                                                           exportReqVO) {
        return storeProductAssureRefMapper.selectList(exportReqVO);
    }

    @Override
    public void saveProductAssureRef(List<Long> assureIdList, Long productId, boolean isAdd) {
        if (!isAdd) {
            //删除旧数据
            storeProductAssureRefMapper.delete(Wrappers.lambdaQuery(StoreProductAssureRefDO.class).eq(StoreProductAssureRefDO::getProductId, productId));
        }
        if (ObjectUtil.isNotEmpty(assureIdList)) {
            List<StoreProductAssureRefDO> collect = assureIdList.stream().map(assureId -> {
                StoreProductAssureRefDO storeProductAssureRef = new StoreProductAssureRefDO();
                storeProductAssureRef.setProductId(productId);
                storeProductAssureRef.setAssureId(assureId);
                return storeProductAssureRef;
            }).collect(Collectors.toList());
            storeProductAssureRefMapper.insertBatch(collect);
        }
    }

    @Override
    public List<Long> getAssureIdListByProductId(Long productId) {
        return storeProductAssureRefMapper.selectList(Wrappers.lambdaQuery(StoreProductAssureRefDO.class).eq(StoreProductAssureRefDO::getProductId, productId)).stream().map(StoreProductAssureRefDO::getAssureId).collect(Collectors.toList());
    }

    @Override
    public List<StoreProductAssureRespVO> getAssureListByProductId(Long productId) {
        List<Long> assureIdList = getAssureIdListByProductId(productId);
        if(ObjectUtil.isNotEmpty(assureIdList)){
            List<StoreProductAssureDO> storeProductAssureList = storeProductAssureService.getStoreProductAssureList(assureIdList);
            return StoreProductAssureConvert.INSTANCE.convertList(storeProductAssureList);
        }
        return null;
    }

}
